import React from 'react'
import { getSortedPostsData } from '../../lib/paginationPosts'
import Page from '../index'
import { useRouter } from 'next/router'
import fs from 'fs'
import path from 'path'

export default function ({ allPostsData, total }) {
  return <Page allPostsData={allPostsData} total={total} />
}
export async function getStaticPaths() {
  const postsDirectory = path.join(process.cwd(), 'posts')
  const files = fs.readdirSync(postsDirectory)
  const pages = Math.ceil(files.length / 10)
  return {
    paths: Array(pages).fill(1).map((item, i) => ({
      params: { page: i + 1 + '' }
    })),
    fallback: false
  }
}
export async function getStaticProps({
  params: { page }
}) {
  const { allPostsData, total } = getSortedPostsData(page)
  return {
    props: {
      allPostsData,
      total
    }
  }
}